﻿package frEngine.shader.filters.vertexFilters
{
    import frEngine.shader.*;
    import frEngine.shader.filters.*;

    public class VertexNormalFilter extends FilterBase
    {

        public function VertexNormalFilter()
        {
            super(FilterType.VertexNormalFilter, FilterPriority.vertexNormal);
            return;
        }// end function

        override public function setRegisterParams(param1:Program3dRegisterInstance) : void
        {
            return;
        }// end function

        override public function get programeId() : String
        {
            return "VertexNormalFilter";
        }// end function

        override public function createVertexCode(param1:Program3dRegisterInstance, param2:int) : String
        {
            var _loc_3:String = null;
            if (param2 >= 4)
            {
                throw new Error("法线最大骨骼数应为3");
            }
            if (param2 > 0)
            {
                _loc_3 = this.createMd5Normal(param1, param2);
            }
            else
            {
                _loc_3 = this.createNoSkinNoraml(param1);
            }
            return _loc_3;
        }// end function

        private function createNoSkinNoraml(param1:Program3dRegisterInstance) : String
        {
            var _loc_2:Array = [new ToBuilderInfo("vf0", "{vnormal}", false, 1), new ToBuilderInfo("vt1", FilterName.T_VetextNormal, false, 1), new ToBuilderInfo("va0", FilterName.NORMAL, false, 3)];
            var _loc_3:* = "" + "mov          \t\tvt1      \tva0         \n" + "mov           \tvf0    \tvt1          \n";
            _loc_3 = param1.toBuild(_loc_3, _loc_2);
            return _loc_3;
        }// end function

        private function createMd5Normal(param1:Program3dRegisterInstance, param2:int) : String
        {
            var _loc_3:* = new Array();
            _loc_3.push(new ToBuilderInfo("va0", FilterName.NORMAL, false, 3));
            _loc_3.push(new ToBuilderInfo("vf0", "{vnormal}", false, 1));
            _loc_3.push(new ToBuilderInfo("vt1", FilterName.T_VetextNormal, false, 1));
            var _loc_4:String = "mov           vt0           vc0 \t\t\t\t\t\t\t\t\n";
            switch(param2)
            {
                case 1:
                {
                    _loc_4 = _loc_4 + ("m33           vt0.xyz       va0           vc[{SKIN_INDICES}.x]\t\t\t\n" + "mul           vt1.xyz       vt0           {SKIN_WEIGHTS}.x   \t\t\t\n");
                    break;
                }
                case 2:
                {
                    _loc_4 = _loc_4 + ("m33           vt0.xyz       va0           vc[{SKIN_INDICES}.x]\t\t\t\n" + "mul           vt1           vt0           {SKIN_WEIGHTS}.x   \t\t\t\n" + "m33           vt0.xyz       va0           vc[{SKIN_INDICES}.y]  \t\t\n" + "mul           vt2           vt0           {SKIN_WEIGHTS}.y         \t\n" + "add           vt1.xyz       vt1           vt2           \t\t\t\t\n");
                    break;
                }
                case 3:
                {
                    _loc_4 = _loc_4 + ("m33           vt0.xyz       va0           vc[{SKIN_INDICES}.x]\t\t\t\n" + "mul           vt1           vt0           {SKIN_WEIGHTS}.x   \t\t\t\n" + "m33           vt0.xyz       va0           vc[{SKIN_INDICES}.y]  \t\t\n" + "mul           vt2           vt0           {SKIN_WEIGHTS}.y         \t\n" + "m33           vt0.xyz       va0           vc[{SKIN_INDICES}.z]  \t\t\n" + "mul           vt3           vt0           {SKIN_WEIGHTS}.z         \t\n" + "add           vt1.xyz       vt1           vt2           \t\t\t\t\n" + "add           vt1.xyz       vt1           vt3           \t\t\t\t\n");
                    break;
                }
                default:
                {
                    break;
                }
            }
            _loc_4 = _loc_4 + "nrm           vt0.xyz       vt1.xyz           \t\t\t\t\t\n";
            _loc_4 = _loc_4 + "mov           vf0    \t\t vt0         \t\t\t\t\t\t\t\n";
            _loc_4 = param1.toBuild(_loc_4, _loc_3);
            return _loc_4;
        }// end function

    }
}
